//***********************************************************
//*
//* File:          Team4.java

//***********************************************************

package paths.g4;

import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;

import paths.*;

public final class Team4 implements IFCPlayer {

    static Random _random;
    static final String _CNAME = "Team4";
    static final Color  _CCOLOR = new Color(0.8f, 0.2f, 0.4f);
    City base, mainbase;
    Maps _maps;
    ArrayList<City> cities;
    ArrayList<Link> links;
//    TimeFunction timeFunction;
    public void register(Maps __maps) throws Exception {
	    _maps = __maps;
	    cities = _maps.getCities();
	    links = _maps.getLinks();
	    //timeFunction = _maps.getTimeFunction();
    }

    public Move move() throws Exception {
	    BufferedImage image;

	    File file = _maps.getBackgroundImage();
	    
	    //to get the background image, do this
	    image = ImageIO.read(file);
	    
	    //get graphics to draw on the image.
	    Graphics2D g = image.createGraphics();

	    
	    g.setFont(new Font(g.getFont().getName(), Font.BOLD, 18));
	    
	    
	    Iterator<City> itCity = cities.iterator();
	    while(itCity.hasNext()){
	    	City city = itCity.next();
	    	System.out.println(city.name);
	    	if(_maps.getStart().equals(city)){
	    		base=city;
	    		mainbase=city;
	    		g.setColor(Color.RED);
	    		g.drawString("Start: "+city.name, city.x, city.y);		    		
	    	}else{
	    		g.setColor(Color.GREEN);
	    		g.drawString(city.name, city.x, city.y);
	    	}
	    }

		g.setColor(Color.BLUE);
	    Iterator<Link> itLink1 = links.iterator();
	    Iterator<Link> itLink;
	    //System.out.println("Travel times at midnight:");
	 while(itLink1.hasNext())
	 { 
		 Link l2=itLink1.next();
		 itLink = links.iterator();
	    int j=0;
	    while(itLink.hasNext()){
	    	Link l = itLink.next();
	    	if(getCityByName(l.src)==base)
	    	{
	    		System.out.println("link starting at "+j+l.src);
	    	
	    	j++;
	    	System.out.println(l.src + " to "+l.dest);
	    	City src = getCityByName(l.src);
	    	
	    	City desttemp = getCityByName(l.dest);
	    	if(desttemp.x>base.x)
	    	{
	    	g.drawLine(src.x, src.y, desttemp.x, desttemp.y);
	    	base=getCityByName(l.dest);
	    	break;
	    	}
	    	}
	    	
	    	if(getCityByName(l.dest)==base)
	    	{
	    		System.out.println("link starting at "+j+l.src);
		    	
		    	j++;
		    	System.out.println(l.src + " to "+l.dest);
		    	City src = getCityByName(l.dest);
		    	
		    	City desttemp = getCityByName(l.src);
		    	if(desttemp.x>base.x)
		    	{
		    	g.drawLine(src.x, src.y, desttemp.x, desttemp.y);
		    	base=getCityByName(l.src);
		    	break;
		    	}
	    		
	    	}
	    }
    }
	    Move RET = new Move(image, "Dumb WSF Map");
	    return RET;
    }

    private City getCityByName(String name){
    	Iterator<City> it = cities.iterator();
    	while(it.hasNext()){
    		City city = it.next();
    		if(name.equalsIgnoreCase(city.name))
    			return city;
    	}
    	return null;
    }
    
    public String name() throws Exception {
        return _CNAME;
    }

    public Color color() throws Exception {
        return _CCOLOR;
    }

    public boolean interactive() throws Exception {
        return false;
    }        

}
